<?php
/**
 *
 * 圈子搜索
 *
 * SearchCircle.class.php
 * Created by IntelliJ IDEA.
 *
 * @author: hjh <hjh@jf.com>
 *
 * Date: 2017/10/26
 * Time: 21:01
 */

namespace Search;

use Circle\Circle;

class SearchCircle extends SearchBase
{
    public static $QUEUE_NAME = 'SearchCircle';
    public static $SEARCH_INDEX = self::CIRCLE_SEARCH;

    const FIELD_CIRCLE_ID = 'circleid';
    const FIELD_CATE_ID = 'cateid';
    const FIELD_USER_ID = 'userid';
    const FIELD_HEAD_URL = 'headurl';
    const FIELD_NAME = 'name';
    const FIELD_DESCRIPTION = 'description';
    const FIELD_TOPIC_COUNT = 'topiccount';
    const FIELD_FOLLOW_COUNT = 'followcount';
    const FIELD_LAST_PUBLISH_TIME = 'lastpublishtime';
    const FIELD_UPDATE_TIME = 'updatetime';
    const FIELD_CREATE_TIME = 'createtime';
    const FIELD_STATUS = 'status';

    const SORT_FIELD_CREATE_TIME = self::FIELD_CREATE_TIME;
    const SORT_FIELD_UPDATE_TIME = self::FIELD_UPDATE_TIME;
    const SORT_FIELD_LAST_PUBLISH_TIME = self::FIELD_LAST_PUBLISH_TIME;

    public static $SEARCH_FIELD_MAP = [
        Circle::FIELD_CIRCLE_ID => [self::FIELD_CIRCLE_ID, self::FIELD_DATA_TYPE_INT],
        Circle::FIELD_CATE_ID => [self::FIELD_CATE_ID, self::FIELD_DATA_TYPE_INT],
        Circle::FIELD_USER_ID => [self::FIELD_USER_ID, self::FIELD_DATA_TYPE_INT],
        Circle::FIELD_HEAD_URL => [self::FIELD_HEAD_URL, self::FIELD_DATA_TYPE_STRING],
        Circle::FIELD_NAME => [self::FIELD_NAME, self::FIELD_DATA_TYPE_STRING],
        Circle::FIELD_DESCRIPTION => [self::FIELD_DESCRIPTION, self::FIELD_DATA_TYPE_STRING],
        Circle::FIELD_TOPIC_COUNT => [self::FIELD_TOPIC_COUNT, self::FIELD_DATA_TYPE_INT],
        Circle::FIELD_FOLLOW_COUNT => [self::FIELD_FOLLOW_COUNT, self::FIELD_DATA_TYPE_INT],
        Circle::FIELD_LAST_PUBLISH_TIME => [self::FIELD_LAST_PUBLISH_TIME, self::FIELD_DATA_TYPE_DATE],
        Circle::FIELD_UPDATE_TIME => [self::FIELD_UPDATE_TIME, self::FIELD_DATA_TYPE_DATE],
        Circle::FIELD_CREATE_TIME => [self::FIELD_CREATE_TIME, self::FIELD_DATA_TYPE_DATE],
        Circle::FIELD_STATUS => [self::FIELD_STATUS, self::FIELD_DATA_TYPE_INT],
    ];

    /**
     * @see SearchBase::Add($id, $info)     新增
     * @see SearchBase::update($id, $info)  更新
     */


    /**
     * @param int   $cateId 分类ID
     * @param array $fields 要返回的字段，参考FIELD_XXXX
     * @param int   $offset 分页起始值
     * @param int   $limit  返回条数
     * @param array $sort   排序 [self::SORT_FIELD_XXXX => self::SORT_TYPE_XXX]
     * @param int   $status 主题状态址，0表示正常，参考Topic
     * @return array
     */
    public static function searchCircleByCate($cateId, $fields = [], $offset = 0, $limit = 20, $sort = [], $status = Circle::STATUS_NORMAL)
    {
        if (empty($sort)) {
            $sort = [
                self::SORT_FIELD_CREATE_TIME => [
                    "order" => self::SORT_TYPE_DESC
                ]
            ];
        }

        if (empty($fields)) {
            $fields = self::buildAllFields();
        }

        $query = [
            "query" => [
                "bool" => [
                    "must" => [
                        [
                            "term" => [
                                self::FIELD_STATUS => $status
                            ]
                        ],
                        [
                            "term" => [
                                self::FIELD_CATE_ID => $cateId
                            ]
                        ]
                    ]
                ]
            ],
            "sort" => $sort,
            "_source" => $fields,
            "from" => $offset,
            "size" => $limit
        ];

        return self::getSearchResults($query, $fields);
    }

    /**
     * 构造全部返回字段
     *
     * @return array
     */
    private static function buildAllFields()
    {
        return [
            self::FIELD_CIRCLE_ID,
            self::FIELD_CATE_ID,
            self::FIELD_USER_ID,
            self::FIELD_HEAD_URL,
            self::FIELD_NAME,
            self::FIELD_DESCRIPTION,
            self::FIELD_TOPIC_COUNT,
            self::FIELD_FOLLOW_COUNT,
            self::FIELD_LAST_PUBLISH_TIME,
            self::FIELD_UPDATE_TIME,
            self::FIELD_CREATE_TIME,
            self::FIELD_STATUS,
        ];
    }

}
